/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.TrainingAddress;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-5下午03:03:27
 */
public class TrainingAddressOperateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse, ActionConfig actionConfig) 
		throws PortletException, IOException {

		String trainingAddressId = actionRequest.getParameter("trainingAddressId");
		String cmd = actionRequest.getParameter("cmd");
		
		String city = actionRequest.getParameter("city");
		String address = actionRequest.getParameter("address");
		String trafficInfo = actionRequest.getParameter("trafficInfo");
		String capacity = actionRequest.getParameter("capacity");
		
		boolean success = false;
		if((trainingAddressId!=null)&&(cmd!=null)&&(city!=null)
				&&(address!=null)&&(trafficInfo!=null)&&(capacity!=null)){
			int capacityInt = DataTypeUtil.parseInt(capacity);
			long trainingAddressIdLong = DataTypeUtil.parseLong(trainingAddressId);	
			
			if((capacityInt>0)&&(trainingAddressIdLong>0)&&(cmd.equals("edit"))){
				EntityManager em = EntityManagerHelper.getEntityManager();
				em.getTransaction().begin();
				TrainingAddress vTrainingAddress = em.find(TrainingAddress.class, trainingAddressIdLong);
				if(vTrainingAddress!=null){
					vTrainingAddress.setCity(city);
					vTrainingAddress.setAddress(address);
					vTrainingAddress.setTrafficInfo(trafficInfo);
					vTrainingAddress.setCapacity(capacityInt);
				}
				em.getTransaction().commit();
				EntityManagerHelper.closeEntityManager();
				success = true;
			}else{
				success = false;
			}
		}else{
			if((trainingAddressId!=null)&&(cmd!=null)){
				long trainingAddressIdLong = DataTypeUtil.parseLong(trainingAddressId);
				if((trainingAddressIdLong>0)&&(cmd.equals("delete"))){
					EntityManager em = EntityManagerHelper.getEntityManager();
					em.getTransaction().begin();
					TrainingAddress vTrainingAddress = em.find(TrainingAddress.class, trainingAddressIdLong);
					if(vTrainingAddress!=null){
						em.remove(vTrainingAddress);
					}
					em.getTransaction().commit();
					EntityManagerHelper.closeEntityManager();
					success = true;		
				}
			}else{
				success = false;
			}
		}
		if(success)return SUCCESS;
		else return FAILED;

	}
}
